package com.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.domain.entity.Address;
import com.domain.entity.User;
import com.domain.req.AddAddressReq;
import com.domain.req.AddressUpdateReq;
import com.domain.req.PageReq;
import com.exception.ServiceException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.AddressMapper;
import com.service.AddressService;
import com.utils.JWTUtils;
import com.utils.ValidatorUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author caishenao
* @description 针对表【address】的数据库操作Service实现
* @createDate 2023-10-12 20:08:11
*/
@Service
@Slf4j
public class AddressServiceImpl implements AddressService{

    @Autowired
    private AddressMapper addressMapper;
    /**
     * 新增收货地址
     *
     * @param addAddressReq 新增收货地址请求
     */
    @Override
    public void addAddress(AddAddressReq addAddressReq) {
        log.info("请求新增收货地址接口，请求入参:{}", JSONObject.toJSONString(addAddressReq));
        // 1. 注解校验
        ValidatorUtils.validateEntity(addAddressReq);

        // 2. 获取当前登录用户
        User currentUser = JWTUtils.getCurrentUser();

        // 3. 拼装新增的信息
        Address address = addAddressReqConversion(addAddressReq,currentUser);

        // 4. 新增
        addressMapper.insertAddress(address);
        log.info("请求新增收货地址接口，处理完成");
    }

    /**
     * 分页查询本人的收货地址
     *
     * @param pageReq 分页请求
     * @param address 查询条件
     */
    @Override
    public PageInfo<Address> listAddresses(PageReq pageReq, Address address) {
        log.info("请求分页查询收货地址接口,请求入参:pageReq:{};address:{}", JSONObject.toJSONString(pageReq), JSONObject.toJSONString(address));

        // 1. 设置当前用户的id
        User currentUser = JWTUtils.getCurrentUser();
        address.setUserId(currentUser.getId());

        // 2. 开启分页
        PageHelper.startPage(pageReq.getPageNum(), pageReq.getPageSize());

        // 3. 查询
        List<Address> addressList = addressMapper.listBySelect(address);

        // 4. 设置分页
        PageInfo<Address> pageInfo = new PageInfo<>(addressList);
        return pageInfo;
    }

    /**
     * 修改收货地址信息
     *
     * @param addressUpdateReq 收货地址修改请求
     */
    @Override
    public void update(AddressUpdateReq addressUpdateReq) {
        // 1. 注解校验
        ValidatorUtils.validateEntity(addressUpdateReq);

        // 2. 查询地址是否存在
        // 获取当前登录用户
        User currentUser = JWTUtils.getCurrentUser();
        Address address = addressMapper.getById(addressUpdateReq.getId(), currentUser.getId());

        if (address == null) {
            // 地址信息为空
            log.error("id:{}对应的地址信息为空!对应的用户为:{}", JSONObject.toJSONString(addressUpdateReq.getId()), JSONObject.toJSONString(currentUser.getId()));
            throw new SecurityException("您的地址信息为空，无法更新!");
        }

        // 3. 将请求转换为地址信息
        address = addressUpdateReqConversion(addressUpdateReq,currentUser);

        // 4. 更新地址信息
        addressMapper.updateSelect(address);
    }

    /**
     * 根据id删除
     *
     * @param id 收货id
     */
    @Override
    public void delete(Integer id) {
        // 1. 判断数据是否存在
        // 获取用户信息
        User currentUser = JWTUtils.getCurrentUser();
        Address address = addressMapper.getById(id, currentUser.getId());
        if (address == null) {
            log.error("收货地址不存在！");
            throw new ServiceException("收货地址不存在，无法删除！");
        }

        // 2. 根据地址id和当前用户id删除
        addressMapper.deleteByIdAndUserId(id, currentUser.getId());
    }

    /**
     * 删除本人所有的收货地址
     */
    @Override
    public void deleteAll() {
        // 1. 获取当前登录用户
        User currentUser = JWTUtils.getCurrentUser();

        // 2. 根据用户id删除收货地址
        addressMapper.deleteByUserId(currentUser.getId());
    }

    /**
     * 根据id查询地址
     *
     * @param id 收货地址id
     * @return 收货地址
     */
    @Override
    public Address getById(Integer id) {
        // 1. 获取当前登陆的用户
        User currentUser = JWTUtils.getCurrentUser();

        // 2.查询并返回
        return addressMapper.getById(id,currentUser.getId());
    }

    /**
     * 查询本人所有收货地址信息
     *
     * @return 本人所有收货信息
     */
    @Override
    public List<Address> list() {
        // 1. 获取当前登录用户
        User currentUser = JWTUtils.getCurrentUser();
        List<Address> list = addressMapper.list(currentUser.getId());
        return list;
    }

    /**
     * 将地址更新请求转换为地址对象
     *
     * @param addressUpdateReq 地址更新请求
     * @param currentUser 当前登录用户
     * @return 地址信息
     */
    private Address addressUpdateReqConversion(AddressUpdateReq addressUpdateReq, User currentUser) {
        Address address = new Address();
        address.setId(addressUpdateReq.getId());
        address.setUserId(currentUser.getId());
        address.setName(addressUpdateReq.getName());
        address.setPhone(addressUpdateReq.getPhone());
        address.setProvince(addressUpdateReq.getProvince());
        address.setCity(addressUpdateReq.getCity());
        address.setArea(addressUpdateReq.getArea());
        address.setSpecific(addressUpdateReq.getSpecific());
        address.setUpdateTime(new Date());
        return address;
    }

    /**
     * 新增地址请求转换
     * @param addAddressReq 新增的地址请求
     * @param currentUser 当前登录用户
     * @return 拼装的地址信息
     */
    private Address addAddressReqConversion(AddAddressReq addAddressReq, User currentUser) {
        Address address = new Address();
        address.setUserId(currentUser.getId());
        address.setName(addAddressReq.getName());
        address.setPhone(addAddressReq.getPhone());
        address.setProvince(addAddressReq.getProvince());
        address.setCity(addAddressReq.getCity());
        address.setArea(addAddressReq.getArea());
        address.setSpecific(addAddressReq.getSpecific());
        address.setCreateTime(new Date());
        return address;
    }
}




